package cn.sumpu.app.wlcity.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import cn.sumpu.app.wlcity.dao.SeckillDao;
import cn.sumpu.app.wlcity.domain.Seckill;
import cn.sumpu.app.wlcity.domain.SeckillPhone;
import cn.sumpu.app.wlcity.domain.SeckillTime;
import cn.sumpu.app.wlcity.exception.AppBusinessException;

public class SeckillDaoImpl extends SqlMapClientDaoSupport implements SeckillDao{
	private static Logger logger = LoggerFactory
	.getLogger(SeckillDaoImpl.class);

	/*插入秒杀活动信息*/
	@Override
	public void insert(Seckill seckill) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().insert("seckill.insert", seckill);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write seckilldatabase failure.", e);
			}
			throw new AppBusinessException("Write seckilldatabase failure.", e);
		}
		
	}
	/*查询秒杀活动信息*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Seckill> getSeckillList(Map<String, Object> map) throws AppBusinessException {
		try {
			return (List<Seckill>) getSqlMapClientTemplate().queryForList(
					"seckill.selectAll",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("ReadSeckill database failure.", e);
			}
			throw new AppBusinessException("ReadSeckill database failure.", e);
		}
	}
	/*查询秒杀活动信息5条*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Seckill> getSeckillListFive(Map<String, Object> map) throws AppBusinessException {
		try {
			return (List<Seckill>) getSqlMapClientTemplate().queryForList(
					"seckill.getSeckillListFive",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("ReadSeckill database failure.", e);
			}
			throw new AppBusinessException("ReadSeckill database failure.", e);
		}
	}
	
	
	/*查询参与用户的信息*/
	@SuppressWarnings("unchecked")
	@Override
	public List<SeckillPhone> getSeckillPhoneList(Map<String, Object> map) throws AppBusinessException {
		try {
			return (List<SeckillPhone>) getSqlMapClientTemplate().queryForList(
					"seckill.getSeckillPhoneList",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("ReadSeckill database failure.", e);
			}
			throw new AppBusinessException("ReadSeckill database failure.", e);
		}
	}
	/*插入秒杀的联系信息*/
	@Override
	public void insertSeckillPhone(SeckillPhone seckillPhone) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().insert("seckill.insertSeckillPhone", seckillPhone);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write insertSeckillPhone failure.", e);
			}
			throw new AppBusinessException("Write insertSeckillPhone failure.", e);
		}
		
	}
	
    /*	插入秒杀时间*/
	@Override
	public void insertSeckillTime(SeckillTime seckillTime) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().insert("seckill.insertSeckillTime", seckillTime);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write seckillTimedatabase failure.", e);
			}
			throw new AppBusinessException("Write seckillTimedatabase failure.", e);
		}
		
	}
	/*查询秒杀活动总数*/
	@Override
	public int getSeckillListCount(Map<String, Object> map) throws AppBusinessException {
		try {
			return  (Integer) getSqlMapClientTemplate().queryForObject("seckill.getSeckillListCount",map);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}
	/*查询参与活动的用户总数*/
	@Override
	public int getSeckillPhoneListCount(String seckillId) throws AppBusinessException {
		try {
			return  (Integer) getSqlMapClientTemplate().queryForObject("seckill.getSeckillPhoneListCount",seckillId);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}
	/*删除秒杀活动*/
	@Override
	public void deleteSeckill(String identifier) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().delete("seckill.deleteSeckill",identifier);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
	}
	/*删除秒杀时间*/
	@Override
	public void deleteSeckillTime(String seckillId) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().delete("seckill.deleteSeckillTime",seckillId);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
	}
	/*查询单个秒杀信息*/
	@Override
	public Seckill selectSeckill(String seckillId) throws AppBusinessException {
		try {
			return (Seckill) getSqlMapClientTemplate().queryForObject(
					"seckill.selectSeckill", seckillId);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("Read database failure.", e);
			}
			throw new AppBusinessException("Read database failure.", e);
		}
	}
/*	更新秒杀信息*/
	@Override
	public void updateSeckill(Seckill seckill) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update("seckill.updateSeckill", seckill);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}
	//查询秒杀时间
	@Override
    public SeckillTime getSeckillTime(String seckillId) throws AppBusinessException{
    	try{
    		return (SeckillTime)getSqlMapClientTemplate().queryForObject(
					"seckill.getSeckillTime", seckillId);
    	}catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("getSeckillTime database failure.", e);
			}
			throw new AppBusinessException("getSeckillTime database failure.", e);
		}

      }
    //查询秒杀电话号码
	@Override
    public SeckillPhone selectSeckillPhoneOld(Map<String, Object> map) throws AppBusinessException{
    	try{
    		return (SeckillPhone)getSqlMapClientTemplate().queryForObject(
					"seckill.selectSeckillPhone",map);
    	}catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectSeckillPhone database failure.", e);
			}
			throw new AppBusinessException("selectSeckillPhone database failure.", e);
		}

      }
    /*	更新秒杀信息*/
	@Override
	public void updateSeckillTime(SeckillTime seckillTime) throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update("seckill.updateSeckillTime", seckillTime);
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateSeckillTime database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}
		
	}
	//查询试试运气的时间值
	@Override
	public SeckillTime getTryLuckTime(String seckillId)
			throws AppBusinessException {
		try{
    		return (SeckillTime)getSqlMapClientTemplate().queryForObject(
					"seckill.getTryLuckTime", seckillId);
    	}catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("getTryLuckTime database failure.", e);
			}
			throw new AppBusinessException("getTryLuckTime database failure.", e);
		}
	}
	//更新每天默认数量
	@Override
	public void updateCount() throws AppBusinessException {
		try {
			getSqlMapClientTemplate().update("seckill.updateCount");
		} catch (DataAccessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateCount database failure.", e);
			}
			throw new AppBusinessException("updateCount database failure.", e);
		}
		
	}
}
